<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Question as question_info;

/*
 * 帮助中心
 * */

class Question extends Basic
{
    // 初始化
    public function initialize()
    {
      parent::_initialize();
    }

    /*
     * 列表
     */
    public function index()
    {
        $question_info = new question_info();
        $token = Input('post.token');
        $key = Input('post.key');
        $page = Input('page',1);
        $limit = Input('limit',10);
        if (!$token) {
            json_fail(data('-5', 'token不能为空!'));
        }
        $where = '';
        if ($key) {
            $where['question'] = ['like', '%' . $key . '%'];
        }
        if ($token == encryption(config('token'))) {
            $list = $question_info->index($where,$page,$limit);
            if ($list) {
                json_success($list);
            } else {
                json_success([]);
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }
    /*
   * 添加
   */
    public function add()
    {
        //实例化数据库
        $question_info = new question_info();
        $token = input('post.token');
        $question = input('post.question');
        $answer = input('post.answer');
    //   $system_manager_id = input('post.system_manager_id');
        if (!$token) {
            json_fail('token不能为空');
        }
        if (!$question) {
            json_fail('问题不能为空');
        }
        if (!$answer) {
            json_fail('答案不能为空');
        }
//        if (!$system_manager_id) {
//            json_fail('管理员ID不能为空');
//        }
        $data = input('post.');
      //  $data['system_manager_id'] = decode($system_manager_id);
        $data['system_manager_time'] = time();
        if ($token == encryption(config('token'))) {
            $id = $question_info->allowField(true)->save($data);
            if (!$id) {
                json_success(data('-1', '添加失败'));
            } else {
                json_success(data('1', '添加成功'));
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }

    /*
     * 修改
     */
    public function edit()
    {
        //实例化数据库
        $question_info = new question_info();
        $id = input('post.id');
        $token = input('post.token');
        $question = input('post.question');
        $answer = input('post.answer');
     //  $system_manager_id = input('post.system_manager_id');

        if (!$id) {
            json_fail(data('-2', 'id不能为空!'));
        }
        if (!$token) {
            json_fail('token不能为空');
        }
        if (!$question) {
            json_fail('问题不能为空');
        }
        if (!$answer) {
            json_fail('答案不能为空');
        }
//        if (!$system_manager_id) {
//            json_fail('管理员ID不能为空');
//        }
        $data = input('post.');
       // $data['system_manager_id'] = decode($system_manager_id);
      $data['system_manager_time'] = time();
        if ($token == encryption(config('token'))) {
            $id = $question_info->allowField(true)->save($data,['id'=>$id]);
            if ($id) {
                json_success(data('1', '修改成功'));
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }


    /*
     * 显示修改内容
     */
    public function show_edit()
    {
        //实例化数据库
        $question_info = new question_info();
        $token = input('post.token');
        $id = input('post.id');
        if (!$id) {
            json_fail(data('-1', 'id不能为空!'));
        }
        if (!$token) {
            json_fail(data('-3', 'token不能为空!'));
        }
        if ($token == encryption(config('token'))) {
            $info = $question_info::get($id);
            if (!$info) {
                json_fail(data('-1', '查询失败'));
            } else {
             //   $info['system_manager'] = Db('system_manager')->where('id',$info['system_manager_id'])->value('username');
              //  $info['system_manager_time'] =date('Y-m-d H:i:s',$info['system_manager_time']);
                json_success($info);
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }

    /*
     * 删除
     */
    public function del()
    {
        //实例化数据库
        $question_info = new question_info();
        $token = input('post.token');
        $id = input('post.id');
        if (!$id) {
            json_fail(data('-1', 'id不能为空!'));
        }
        if (!$token) {
            json_fail(data('-3', 'token不能为空!'));
        }
        if ($token == encryption(config('token'))) {
            $data['status'] = 0;
            $id = $question_info->where('id', $id)->update($data);
            if (!$id) {
                json_fail(data('-1', '删除失败'));
            } else {
                json_success(data('1', '删除成功'));
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }


    /*
    * 批量删除
    */
    public function batch_remove()
    {
        //实例化数据库
        $question_info = new question_info();
        $token = input('post.token');
        $id = input('post.id/a');
        if (!$id) {
            json_fail(data('-1', 'id不能为空!'));
        }
        if (!$token) {
            json_fail(data('-3', 'token不能为空!'));
        }
        if ($token == encryption(config('token'))) {
            $data['status'] = 0;
            $id = $question_info->where('id','in', $id)->update($data);
            if (!$id) {
                json_fail(data('-1', '删除失败'));
            } else {
                json_success(data('1', '删除成功'));
            }
        } else {
            json_fail(data('-3', 'token验证错误!'));
        }
    }


}